﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Syndication;
using NLBS.Data;
using NLBS.Framework;
using NLBS.Data.Business;

namespace NLBS.Web.HttpHandlers
{
    public class RssHandler : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            
            RssWriter rss;
            CommentRssWriter commentRss;
            context.Response.ContentType = "text/xml";
            context.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            if (context.Request.QueryString["cat"] != null)
            {
                rss = new RssWriter(ArticleData.GetPageArticleByCatID(1, 10, Convert.ToInt32(context.Request.QueryString["cat"].ToString())), DateTime.Now, true);
            }
            else if (context.Request.QueryString["selected"] != null)
            {
                rss = new RssWriter(Cacher.GetPageArticleBySelected(1, 10), DateTime.Now, true);
            }
            else if (context.Request.QueryString["q"] != null && context.Request.QueryString["id"] == null)
            {

                commentRss = new CommentRssWriter(CommentData.GetPageComment(1, 10,CacheDuration.Long), DateTime.Now, true);
                context.Response.Write(commentRss.Xml.ToString());
                return;
            }

            else if (context.Request.QueryString["q"] != null && context.Request.QueryString["id"]!=null)
            {

                commentRss = new CommentRssWriter(CommentData.GetCommentListByArticleID(Convert.ToInt32(context.Request.QueryString["id"]), CacheDuration.Long), DateTime.Now, true);
                context.Response.Write(commentRss.Xml.ToString());
                return;
            }
            else
            {
                rss = new RssWriter(Cacher.GetRssArticle(10, CacheDuration.Long), DateTime.Now, true);
            }
            context.Response.Write(rss.Xml.ToString());
        }

        #endregion
    }
}
